package com.bluetrum.abmate;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bluetrum.abmate.databinding.ActivityWebviewBinding;

public class WebViewActivity extends AppCompatActivity {

    public static final String WEB_URL = "WEB_URL";

    ActivityWebviewBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityWebviewBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Intent intent = getIntent();
        String webUrl = intent.getStringExtra(WEB_URL);
        if (webUrl != null) {
            binding.webView.setWebViewClient(webViewClient);
            binding.webView.setWebChromeClient(webChromeClient);
            // 禁用长按复制粘贴
            binding.webView.setOnLongClickListener(v -> true);
            binding.webView.setLongClickable(false);
            binding.webView.setHapticFeedbackEnabled(false);
            binding.webView.loadUrl(webUrl);
        }
    }

    //WebViewClient主要帮助WebView处理各种通知、请求事件
    private WebViewClient webViewClient = new WebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {//页面加载完成
            binding.webProgressBar.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {//页面开始加载
            binding.webProgressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url == null) return false;

            //处理http和https开头的url
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
            handler.proceed(); // 接受网站证书
        }

        @Override
        public void onReceivedError(final WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            binding.webProgressBar.setVisibility(View.INVISIBLE);
            // 显示空白页
//            view.loadUrl("about:blank");

//            webView.clearHistory();
        }
    };

    // WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等
    private WebChromeClient webChromeClient = new WebChromeClient(){
        //获取网页标题
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);

            // 设置标题
            if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle(title);
            }
        }

        //加载进度回调
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            binding.webProgressBar.setProgress(newProgress);
        }
    };

}
